package com.jt;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisConnection;

import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@SpringBootTest
public class StringRedis {
     @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //连接测试
    @Test
   public void testTemplate(){
        RedisConnection connection = stringRedisTemplate.getConnectionFactory().getConnection();
        String ping = connection.ping();
        System.out.println(ping);
    }

    //string测试一：以json格式存储对象
    @Test
    void testString2() throws JsonProcessingException {
        //创建一个map集合存储
        HashMap<String, String> map = new HashMap();
        map.put("id", "101");
        map.put("user", "lxd");
        //map转换json
        String jsonStr = new ObjectMapper().writeValueAsString(map);
        //
        ValueOperations<String, String> value = stringRedisTemplate.opsForValue();
        value.set("userJsonStr", jsonStr);
        jsonStr= value.get("userJsonStr");
        Map readValue = new ObjectMapper().readValue(jsonStr, Map.class);
        System.out.println(readValue);//{id=101, user=lxd}

    }
    //string测试二：
    @Test
    void testString1() throws InterruptedException {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();//获取redis值对象
        ops.set("x", "100");
        ops.set("y", "200", 1, TimeUnit.SECONDS);//生效1秒
        TimeUnit.SECONDS.sleep(1);//休眠1秒
        ops.increment("x");
        String x = ops.get("x");//101
        System.out.println(x);
        String y = ops.get("y");
        System.out.println(y);//null

    }
    //Hash测试一：
    @Test
    void  testHash(){
        HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();
        hash.put("s", "id", "100");
        hash.put("s", "username", "tom");
        hash.put("s", "state", "true");
        List<Object> user = hash.values("s");
        System.out.println(user);//[100, tom, true]

    }
    //Hash测试二
    @Test
    void testHash2(){

        HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();

        HashMap<String, String> map = new HashMap();
        map.put("id", "123");
        map.put("username", "mok");

        hash.putAll("map", map);//拉取存储
        hash.put("map","content","今天出去玩");//put(h,hk,hv)添加
        Object o = hash.get("map", "id");
        System.out.println(o);//123
        Map<Object, Object> entries = hash.entries("map");
        System.out.println("entrys="+entries);//entrys={id=123, username=mok, content=今天出去玩}


    }

}
